<?php
/*
---------------------------------------------------------------------
- 功能描述: 润滑油产品管理
- 文件名:xuanyouProductController.php
- 版本: 1.0
- 原作者:robot2014/9/24
---------------------------------------------------------------------
*/

class xuanyouProductController extends Base
{

	/**
	* 构造函数
	*
	* @access public
	* @param
	* @return void
	*/
	public function __construct()
	{
		//是否登录
		if(!Admin::isAdminLogin())
		{
			Common::header("/login.html");
		}

		//自定义分页
		Admin::loadCustom();
	}

	/**
	* 选油日志
	*
	* @access public
	* @param  void
	* @return void
	*/
	public function searchAction()
	{
		switch(parent::$args['act'])
		{
			case "ip": $aKey = array("ip"=>parent::$args['z']); break;
			case "cookie": $aKey = array("cookie"=>parent::$args['z']); break;
			default: $aKey = array();
		}

		$this->page->PerPage = $_SESSION['libvar']['pageset'];
		$this->page->Page = parent::$args['page'];

		$aRes = $this->product_dataModule->listdata($aKey, $this->page);

		$this->smarty->assign("infolist", $aRes);#print_r($aRes);
		$this->smarty->assign("pagecount", $this->page->Count);
		$this->smarty->assign("page", $this->page->getPage());
		$this->smarty->display("admin/product/product_list.html");
	}

	/**
	* 删除反馈
	*
	* @access public
	* @param  void
	* @return void
	*/
	public function optAction()
	{
		//关闭信息
		if($_POST['act'] == '关闭')
		{
			if(!$_POST['ids'][0])
			{
				Common::show("请至少选择1条信息。");
			}
			foreach($_POST['ids'] as $id)
			{
				$this->product_dataModule->setState($id, 0);
			}
		}
		else if(parent::$args['act'] == 'close')
		{
			$this->product_dataModule->setState(parent::$args['id'], 0);
		}
		//开放信息
		else if($_POST['act'] == '开放')
		{
			if(!$_POST['ids'][0])
			{
				Common::show("请至少选择1条信息。");
			}
			foreach($_POST['ids'] as $id)
			{
				$this->product_dataModule->setState($id, 1);
			}
		}
		else if(parent::$args['act'] == 'open')
		{
			$this->product_dataModule->setState(parent::$args['id'], 1);
		}

		//修改字段
		else if($_POST['ajaxedit'])
		{
			$r = $this->product_dataModule->edit($_POST['id_field'], $_POST['value']);
			echo json_encode(array("success"=>$r ? 1 : 0));
			exit;
		}

		Common::forward($_SERVER['HTTP_REFERER'], "parent");
	}

	/**
	* 导入
	*
	* @access public
	* @param  void
	* @return void
	*/
	public function importAction()
	{
		$this->page->PerPage = $_SESSION['libvar']['pageset'];
		$this->page->Page = parent::$args['page'];
		if($_POST['submit'])
		{
			if(is_uploaded_file($_FILES['upfile']['tmp_name']))
			{
				$aContent = file($_FILES['upfile']['tmp_name']);
				
				$line = 0;
				foreach($aContent as $item)
				{
					$line++;
					$item = str_replace(array("\r", "\n"), "", $item);
					$item = trim(iconv("gb2312", "utf-8", $item));
					$item_tmp = explode(",", $item);

					//去除标题
					if($item_tmp[0] == '规格型号' || $item_tmp[0] == '')
					{
						continue;
					}

					//数据合法性检查
					$r = $this->product_dataModule->check($item_tmp);
					if($r !== true)
					{
						$aResError[$line] = $r;
					}
					$aResData[$line] = $item_tmp;
				}

				if(empty($aResError))
				{
					foreach($aResData as $key => $val)
					{
						$r = $this->product_dataModule->add($val);
						if($r !== true)
						{
							$aResError[$key] = "已存在";
							$errcount++;
						}
						else
						{
							$okcount++;
						}
					}
				}
			}
			$this->smarty->assign("show", 1);
			$this->smarty->assign("okcount", intval($okcount));
			$this->smarty->assign("errcount", intval($errcount));
			$this->smarty->assign("errorlist", $aResError);
		}

		$this->smarty->display("admin/product/import.html");
	}
}
?>